﻿#pragma once
#include<iostream>
#include<assert.h>
#include<list>
using namespace std;
namespace YZK
{
	template<class K>
	struct list_node
	{
		K _data;
		list_node<K>* _next;
		list_node<K>* _prev;
		list_node(const K& data = K())
			:_data(data)
			, _next(nullptr)
			, _prev(nullptr)
		{}
	};
	template<class K,class Ref,class Ptr>
	struct list_iterator
	{
		typedef list_node<K> Node;
		typedef list_iterator<K,Ref,Ptr> Self;
		Node* _PNode;
		list_iterator(Node* PNode)
			:_PNode(PNode)
		{
		}
		Ref operator*()
		{
			return _PNode->_data;
		}
		Ptr operator->()
		{
			return &_PNode->_data;
		}
		//ǰ++
		Self& operator++()
		{
			_PNode = _PNode->_next;
			return *this;
		}
		//ǰ--
		Self& operator--()
		{
			_PNode = _PNode->_prev;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(*this);
			_PNode = _PNode->_next;
			return tmp;
		}
		Self operator--(int)
		{
			Self tmp(*this);
			_PNode = _PNode->_prev;
			return tmp;
		}
		bool operator!=(const Self& s) const
		{
			return _PNode != s._PNode;
		}
		bool operator==(const Self& s) const
		{
			return _PNode == s._PNode;
		}
	};
	template<class K>
	struct list_const_iterator
	{
		typedef list_node<K> Node;
		typedef list_const_iterator<K> Self;
		Node* _PNode;
		list_const_iterator(Node* PNode)
			:_PNode(PNode)
		{
		}
		const K& operator*()
		{
			return _PNode->_data;
		}
		const K* operator->()
		{
			return &_PNode->_data;
		}
		//ǰ++
		Self& operator++()
		{
			_PNode = _PNode->_next;
			return *this;
		}
		//ǰ--
		Self& operator--()
		{
			_PNode = _PNode->_prev;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(*this);
			_PNode = _PNode->_next;
			return tmp;
		}
		Self operator--(int)
		{
			Self tmp(*this);
			_PNode = _PNode->_prev;
			return tmp;
		}
		bool operator!=(const Self& s) const
		{
			return _PNode != s._PNode;
		}
		bool operator==(const Self& s) const
		{
			return _PNode == s._PNode;
		}
	};
	template<class K>
	class list
	{
		typedef list_node<K> Node;
	public:
		//typedef list_iterator<K> iterator;
		//typedef list_const_iterator<K> const_iterator;
		typedef list_iterator<K, K&, K*> iterator;
		typedef list_iterator<K, const K&, const K*> const_iterator;
		iterator begin()
		{
			//iterator it(_head->_next);
			//return it;
			//return iterator(_head->_next);
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin() const
		{
			return _head->_next;
		}
		const_iterator end() const
		{
			return _head;
		}
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list()
		{
			empty_init();
		}
		list(initializer_list<K> il)
		{
			empty_init();
			for (auto& e : il)
			{
				push_back(e);
			}
		}
		list(const list<K>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}
		list<K>& operator=(list<K> lt)
		{
			swap(lt);
			return *this;
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			auto it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		void swap(list<K>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}
		void push_back(const K& x)
		{
			//Node* newnode = new Node(x);
			//Node* tail = _head->_prev;
			//tail->_next = newnode;
			//newnode->_prev = tail;
			//newnode->_next = _head;
			//_head->_prev = newnode;
			//++_size;
			insert(end(), x);
		}
		void push_front(const K& x)
		{
			insert(begin(), x);
		}
		iterator insert(iterator pos, const K& x)
		{
			Node* cur = pos._PNode;
			Node* prev = cur->_prev;
			Node* newnode = new Node(x);
			newnode->_next = cur;
			cur->_prev = newnode;
			newnode->_prev = prev;
			prev->_next = newnode;
			++_size;
			return newnode;
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* prev = pos._PNode->_prev;
			Node* next = pos._PNode->_next;
			prev->_next = next;
			next->_prev = prev;
			delete pos._PNode;
			--_size;
			return next;
		}
		size_t size() const
		{
			return _size;
		}
		bool empty() const
		{
			return _size == 0;
		}
	private:
		Node* _head;
		size_t _size;
	};
	struct kk
	{
		int _k1 = 1;
		int _k2 = 1;
	};
	template<class container>
	void print_container(const container& con)
	{
		//list<int>::const_iterator it = con.begin();
		typename container::const_iterator it = con.begin();
		//auto it = con.begin();
		while (it != con.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
		for (auto e : con)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void test_list1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		list<int>::iterator it = lt.begin();
		//cout << sizeof(it) << endl;
		while (it != lt.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;
		print_container(lt);
		list<kk> ltk;
		ltk.push_back(kk());
		ltk.push_back(kk());
		ltk.push_back(kk());
		ltk.push_back(kk());
		list<kk>::iterator itk = ltk.begin();
		while (itk != ltk.end())
		{
			//cout << (*itk)._k1 << ":"<<(*itk)._k2<<endl;
			cout << itk->_k1 << ":" << itk->_k2 << endl;
			//cout << itk.operator->()->_k1 << ":" << itk.operator->()->_k2 << endl;
			++itk;
		}
		cout << endl;
	}
	void test_list2()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		list<int>::iterator it = lt.begin();
		lt.insert(it, 10);
		*it += 100;
		print_container(lt);
		it = lt.begin();
		while (it != lt.end())
		{
			if (*it % 2 == 0)
			{
				it=lt.erase(it);
			}
			else
			{
				++it;
			}
		}
		print_container(lt);
	}
	void test_list3()
	{
		list<int> lt1;
		lt1.push_back(1);
		lt1.push_back(2);
		lt1.push_back(3);
		lt1.push_back(4);
		list<int> lt2(lt1);
		print_container(lt1);
		print_container(lt2);
		list<int> lt3;
		lt3.push_back(10);
		lt3.push_back(20);
		lt3.push_back(30);
		lt3.push_back(40);
		lt1 = lt3;
		print_container(lt1);
		print_container(lt3);
	}
	void func(const list<int>& lt)
	{
		print_container(lt);
	}
	void test_list4()
	{
		list<int> lt1({ 1,2,3,4,5,6 });
		list<int> lt2 = { 1,2,3,4,5,6 };
		const list<int>& lt3 = { 1,2,3,4,5,6,7 };
		print_container(lt2);
		func(lt1);
		func({ 1,2,3,4,5,6 });
		//list<int> lt1 = { 1,2,3,4,5,6 };
		//print_container(lt1);
		//auto al = { 10,20,30 };
		//initializer_list<int> il = { 10,20,30 };
		//cout << typeid(il).name() << endl;
		//cout << sizeof(il) << endl;
	}
}